package others;

import java.util.concurrent.Semaphore;

/**
 * 信号量
 *
 *  1.限流
 *
 */
public class SemaphoreTest {
    public static void main(String[] args) {
        //控制能运行的线程数
        Semaphore s = new Semaphore(1);
        new Thread(()->{
            try {
                s.acquire();
                System.out.println("T1 running...");
                Thread.sleep(200);
                System.out.println("T1 running...");
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                s.release();
            }
        }).start();

        new Thread(()->{
            try {
                s.acquire();
                System.out.println("T2 running...");
                Thread.sleep(200);
                System.out.println("T2 running...");
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                s.release();
            }
        }).start();

        //T1执行完了后，T2才能执行
    }
}
